import {
    Get_USER_DATA,
    UPDATA_USER_DATA
} from "./mutations-types.js"

export default{
    namespaced: true,//开启带命名空间的模块
    state:{
        userInfo:[],
            //储存用户信息
            //哪个什么Data的
        nulluserInfo:[]
        
    },
    getters:{
        // localstore存储数据  
         Set(localstoreuserInfo,value,sTime){
            let obj = {
                data: value,
                time: Date.now(),
                storageTime: sTime||99999999999999  //无传入则默认值
            }
            localstorage.setItem(localstoreuserInfo, JSON.stringify(obj))
        },
        
        // localstore存储取值
        Get(key){
            let obj = localstorage.getItem(key)
            //无此localstorage
            if(obj==null){
                //无这个object则新建一个localstorage
                Set(key,state.nulluserInfo,6000)//后期改  userData  
            }
            obj = JSON.parse(obj);//解析
            //过期
            if(Date.now()-obj.time>obj.storageTime){
                localStorage.removeItem(key);
                return state.nulluserInfo;//过期返回空
            }
            return obj.data//有效返回非空
        }
        //保存语法//localStorage.setItem("key","value");       
        //读取数据//let data = localStorage.getItem("key");     
        //删除数据语法//localStorage.removeItem("key")        
        //使用//Set('name','xxx',6000)  // 仅存储6s    
        //Get('name')  // 6s后取值为null
    },
    mutations:{
        //读取
            /*getUserData(state,token){
            let tokens=toString(token);
            state.userInfo=this.Get(tokens);//vuexuserInfo
            }, */
            [Get_USER_DATA](state,token){
                let tokens=toString(token);
                state.userInfo=this.Get(tokens);//vuexuserInfo
            },
        //新增
        //[ADD_USER_DATA]
        //修改
        //[MODIFY_USER_DATA]
        //删除
        //[DELETE_USER_DATA]
        //更新数据
        [UPDATA_USER_DATA](state,array){
            Set('vuexuserInfo',array,99999999)
            state.userInfo=array;
        },
    },
    getters:{

    },
/*     actions:{
        
    },
     */
}